<template>
  <el-dialog
    title="修改密码"
    :visible.sync="dialogVisible"
    :show-close="closeable"
    :before-close="cancel"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    class="vc-dialog-form"
    width="500px"
  >
    <el-form
      ref="modifyPwd"
      :model="modifyPwd"
      :rules="rules"
      label-width="100px"
      size="small"
      v-if="modifyPwdFormShow"
    >
      <el-input type="text" v-show="false"></el-input
      ><el-input type="password" v-show="false"></el-input>
      <el-form-item label="用户名">
        <el-input v-model="username" disabled></el-input>
      </el-form-item>

      <el-form-item label="原密码" prop="oldPwd">
        <el-input
          v-model="modifyPwd.oldPwd"
          maxlength="20"
          placeholder="请输入"
          class="show-password"
          show-password
          clearable
          @clear="$refs.modifyPwd.validateField('oldPwd')"
        ></el-input>
      </el-form-item>

      <el-form-item label="新密码" prop="newPwd">
        <el-input
          v-model="modifyPwd.newPwd"
          maxlength="20"
          placeholder="请输入"
          class="show-password"
          show-password
          clearable
          @clear="$refs.modifyPwd.validateField('newPwd')"
        ></el-input>
      </el-form-item>

      <el-form-item label="确认密码" prop="checkNewPwd">
        <el-input
          v-model="modifyPwd.checkNewPwd"
          maxlength="20"
          placeholder="请输入"
          class="show-password"
          show-password
          clearable
          @clear="$refs.modifyPwd.validateField('checkNewPwd')"
        ></el-input>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button size="small" v-show="closeable" @click="cancel"
        >取消</el-button
      >
      <el-button type="primary" size="small" :disabled="disabled" @click="sure"
        >确定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
import store from '@/store'

export default {
  name: 'modifyPwd',
  data () {
    var validatePass = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入新密码'))
        return
      }
      const reg = /^[\s\S]{8,20}$/
      if (!reg.test(value)) {
        callback(new Error('请输入8 ~ 20个字符'))
        return
      }
      let msg = this.GLOBAL.pwdHandler.checkPwdByName(value, this.username)
      if (msg) {
        callback(new Error(`${msg}`))
        return
      }
      this.$api.findSafeConfigByCode({
        data: {
          code: this.GLOBAL.pwdHandler.SAFE_CONFIG_CODE
        }
      }).then(res => {
        if (!res.success || !res.data) {
          callback()
          return
        }
        let msg = this.GLOBAL.pwdHandler.checkPwdByRules(value, res.data.val)
        if (msg) {
          callback(new Error(`必须包含${msg}`))
          return
        }
        this.modifyPwd.checkNewPwd && this.$refs.modifyPwd.validateField('checkNewPwd')
        callback()
      }).catch(() => {
        callback()
      })
    }
    var validatePass2 = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请再次输入新密码'))
      } else if (value !== this.modifyPwd.newPwd) {
        callback(new Error('两次输入新密码不一致'))
      } else {
        callback()
      }
    }
    return {
      username: '',
      dialogVisible: false,
      closeable: null,
      modifyPwd: {
        oldPwd: '',
        newPwd: '',
        checkNewPwd: ''
      },
      rules: {
        oldPwd: [
          { required: true, message: '请输入原密码', trigger: 'blur' }
        ],
        newPwd: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
        checkNewPwd: [
          { required: true, validator: validatePass2, trigger: 'blur' }
        ]
      },
      disabled: false,
      modifyPwdFormShow: true
    }
  },
  watch: {
    dialogVisible (val) {
      if (!val) return
      this.disabled = false
      this.$nextTick(() => {
        this.$refs.modifyPwd && this.$refs.modifyPwd.resetFields()
        this.modifyPwdFormShow = false
        setTimeout(() => { this.modifyPwdFormShow = true }, 0)
      })
    }
  },
  methods: {
    /**
     * 确定
     */
    sure () {
      if (this.disabled) {
        return
      }
      this.$refs.modifyPwd.validate((valid) => {
        if (!valid) {
          return false
        }
        this.disabled = true
        this.$api.modifyPasswd({
          data: {
            oldPwd: this.ENCRYPT.RSAencrypt(this.modifyPwd.oldPwd),
            newPwd: this.ENCRYPT.RSAencrypt(this.modifyPwd.newPwd)
          }
        }).then(res => {
          if (!res.success) {
            this.disabled = false
            return
          }
          this.dialogVisible = false
          store.dispatch('logout')
        }).catch(() => {
          this.disabled = false
        })
      })
    },
    /**
     * 取消
     */
    cancel () {
      this.dialogVisible = false
    }
  }
}
</script>
